package com.cf.user.client;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cf.system.feign.RoleFeign;
import com.cf.user.dto.UserDTO;
import com.cf.user.entity.User;
import com.cf.user.feign.UserFeign;
import com.cf.user.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ChenFeng
 * @date 2021/11/8 2:51 下午
 * @description
 */
@RestController
@AllArgsConstructor
public class UserClient implements UserFeign {

    private final UserService userService;

    private final RoleFeign roleFeign;

    @Override
    public UserDTO getUserByUsername(String username) {
        UserDTO result = new UserDTO();
        User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, username));
        List<String> roleNames = roleFeign.getRoleNamesByRoleIds(user.getRoleId());
        BeanUtils.copyProperties(user, result);
        result.setRoles(roleNames);
        return result;
    }
}
